package com.example.administrator.wallet.view.activity.find.cointrans;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.androidkun.xtablayout.XTabLayout;
import com.example.administrator.wallet.R;
import com.example.administrator.wallet.adapter.MyPagerAdapter;
import com.example.administrator.wallet.base.BaseActivity;
import com.example.administrator.wallet.constant.Api;
import com.example.administrator.wallet.custom.MyWebSocketService;
import com.example.administrator.wallet.intf.IRequestCallback;
import com.example.administrator.wallet.intf.IRequestManager;
import com.example.administrator.wallet.model.bean.CoinMarketBean;
import com.example.administrator.wallet.utils.RequestFactory;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.example.administrator.wallet.view.fragment.quotation.BTCFragment;
import com.example.administrator.wallet.view.fragment.quotation.ETHFragment;
import com.example.administrator.wallet.view.fragment.quotation.SCNYFragment;
import com.example.administrator.wallet.view.fragment.quotation.SelfFragment;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Effect:
 * <p>
 * Author:  CaiYapeng
 * <p>
 * Time  :  2018/8/16.
 */
public class CoinTransactionActivity extends BaseActivity {
    @Bind(R.id.finish_img)
    ImageButton finishImg;
    @Bind(R.id.view_title)
    TextView viewTitle;
    @Bind(R.id.tab)
    XTabLayout tab;
    @Bind(R.id.pager)
    ViewPager pager;
    private List<String> tab_datas;
    private List<Fragment> pager_datas;

    private IRequestManager reqequestManager;
    private Gson mGson;
    String token;
    private CoinMarketBean coinMarketBean;

    private MyWebSocketService myWebSocketService;
    private ServiceConnection serviceConnection;

    @Override
    public void init() {
        initView();
        initData();
        initAdapter();

    }

    private void initAdapter() {

    }

    private void initData() {
        tab_datas = new ArrayList<>();
        pager_datas=new ArrayList<>();

        tab_datas.clear();
        pager_datas.clear();

        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                myWebSocketService = ((MyWebSocketService.ServiceBinder) service).getService();
                myWebSocketService.join();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };

        bindService(new Intent(this,MyWebSocketService.class),serviceConnection, Context.BIND_AUTO_CREATE);


        reqequestManager = RequestFactory.getReqequestManager();
        token = SharedPrefsUtil.getValue(this,"wallet","token","");
        mGson = new Gson();

        reqequestManager.httpPost(Api.BaseUrl + Api.Get_Coin_Market, Api.getCoinMarket(token), new IRequestCallback() {
            @Override
            public void onSuccess(String Response) {
                coinMarketBean = mGson.fromJson(Response,CoinMarketBean.class);
                int code = coinMarketBean.getCode();
                if (code == 200){

                    tab_datas.add(getString(R.string.zixuan));
                    SelfFragment selfFragment = new SelfFragment();
                    Bundle bundle1 = new Bundle();
                    bundle1.putString("detailList","mySelf");
                    selfFragment.setArguments(bundle1);
                    pager_datas.add(selfFragment);

                    for (CoinMarketBean.DataBean dataBean : coinMarketBean.getData()){
                        tab_datas.add(dataBean.getMain());
                        BTCFragment btcFragment = new BTCFragment();
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("detailList",dataBean);
                        btcFragment.setArguments(bundle);
                        pager_datas.add(btcFragment);
                    }

                } else {

                    tab_datas.add(getString(R.string.zixuan));
                    tab_datas.add( "USDT");
                    tab_datas.add( "BTC");
                    tab_datas.add( "ETH");

                    pager_datas.add(new SelfFragment());
                    pager_datas.add(new BTCFragment());
                    pager_datas.add(new BTCFragment());
                    pager_datas.add(new BTCFragment());
                }

                MyPagerAdapter adapter=new MyPagerAdapter(CoinTransactionActivity.this,tab_datas,pager_datas);
                pager.setAdapter(adapter);
                pager.setClickable(false);
                tab.setupWithViewPager(pager);
            }

            @Override
            public void onFailure(String error) {
                Log.e("getCoinMarketError",error);
            }
        });
    }

    public int sendMeessage(String method, JSONArray jsonArray){
        int id = myWebSocketService.sendMessage(method,jsonArray);
        return id;
    }

    private void initView() {
        viewTitle.setText(R.string.bbtransaction);
        finishImg.setVisibility(View.VISIBLE);
    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_cointransaction;
    }


    @OnClick(R.id.finish_img)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(serviceConnection);
    }
}
